package com.liangzi.service_core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liangzi.liangzi_common.result.R;
import com.liangzi.service_core.pojo.bo.SnowflakeManager;
import com.liangzi.service_core.pojo.entity.VisitMedicalCard;
import com.liangzi.service_core.pojo.vo.VisitMedicalCardVO;
import com.liangzi.service_core.service.VisitMedicalCardService;
import com.liangzi.service_core.service.VisitPatienterService;
import com.liangzi.servicebase.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 就诊卡表 前端控制器
 * </p>
 *
 * @author zhj
 * @since 2021-12-13
 */
@Api(description = "就诊卡接口")
@RestController
@RequestMapping("/web/visitMedicalCard")
@Slf4j
public class VisitMedicalCardController {

    @Autowired
    private VisitMedicalCardService visitMedicalCardService;
    @Autowired
    private VisitPatienterService visitPatienterService;
    @Autowired
    private RedisUtil redisUtil;



    // 判断当前用户是否有 就诊卡
    @ApiOperation("判断当前用户是否有 就诊卡")
    @GetMapping("isHaveMedicalCard/{rdSession}")
    public R isHaveMedicalCard(@PathVariable("rdSession") String rdSession){
        // 判断是否登录
        String userId = (String) redisUtil.get("session:" + rdSession);
        if (userId == null) {
            return  R.error().message("请重新登录");
        }
        String card =  visitMedicalCardService.medicalCardExistByUserId(userId);
        return card == null ? R.error().data("haveCard", false).message("没有就诊卡") : R.ok().data("haveCard", true).data("cardId", card).message("已有就诊卡");

    }



    // 添加就诊卡
    @ApiOperation("添加就诊卡 ")
    @PutMapping("/web/insertMedicalCard/{rdSession}")
    public R insertMedicalCard(
            @PathVariable("rdSession") String rdSession,
            @RequestBody
            @Valid VisitMedicalCardVO visitMedicalCardVO){
        // 判断是否登录
        String userId = (String) redisUtil.get("session:" + rdSession);
        if (userId == null) {
            return  R.error().message("请重新登录");
        }
        String  cardId = visitMedicalCardService.saveCard(userId,visitMedicalCardVO);
        return cardId != null ? R.ok().data("cardId",cardId).message("添加成功") : R.error().message("添加失败");
    }













}

